<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="showSearch">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
          <el-form-item label="模板编号" prop="templateId">
            <el-input v-model="queryParams.templateId" placeholder="请输入模板编号" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="发送状态" prop="sendStatus">
            <el-select v-model="queryParams.sendStatus" placeholder="请选择发送状态" clearable>
              <el-option
                v-for="dict in sys_mail_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="发送时间" style="width: 308px">
            <el-date-picker
                v-model="daterangeSendTime"
                value-format="YYYY-MM-DD HH:mm:ss"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :default-time="[new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 1, 1, 23, 59, 59)]"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <el-table v-loading="loading" :data="mailLogList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="编号" align="center" prop="id" v-if="true" />
        <el-table-column label="发送时间" align="center" prop="sendTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.sendTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="接收邮箱" align="center" prop="toMail" />
        <el-table-column label="邮件标题" align="center" prop="templateTitle" />
        <el-table-column label="发送状态" align="center" prop="sendStatus">
          <template #default="scope">
            <dict-tag :options="sys_mail_status" :value="scope.row.sendStatus"/>
          </template>
        </el-table-column>
        <el-table-column label="发送邮箱地址" align="center" prop="fromMail" />
        <el-table-column label="模板编号" align="center" prop="templateId" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="详情" placement="top">
              <el-button link type="primary" icon="View" @click="open(scope.row.id)" v-hasPermi="['system:mailLog:query']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <pagination
          v-show="total>0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
      />
    </el-card>
    <!-- 弹出详情信息 -->
    <el-dialog title="详情" v-model="dialogVisible" width="800px" >
      <el-descriptions
        direction="vertical"
        :column="4"
        :size="size"
        ref="mailLogFormRef"
        :model="detailData"
        border
      >
        <el-descriptions-item label="编号" >{{ detailData.id }}</el-descriptions-item>
        <el-descriptions-item label="发送时间">{{ detailData.sendTime }}</el-descriptions-item>
        <el-descriptions-item label="接受邮箱">{{ detailData.toMail }}</el-descriptions-item>
        <el-descriptions-item label="发送状态">
          <template #default="scope">
            <dict-tag :options="sys_mail_status" :value="detailData.sendStatus"/>
          </template>
        </el-descriptions-item>
        <el-descriptions-item label="发送异常" :span="2">{{ detailData.sendException }}</el-descriptions-item>
        <el-descriptions-item label="发送邮箱地址">{{ detailData.fromMail }}</el-descriptions-item>
        <el-descriptions-item label="模板编号">{{ detailData.templateId }}</el-descriptions-item>
        <el-descriptions-item label="模板编码">{{ detailData.templateCode }}</el-descriptions-item>
        <el-descriptions-item label="模版发送人名称">{{ detailData.templateNickname }}</el-descriptions-item>
        <el-descriptions-item label="邮件内容">{{ detailData.templateContent }}</el-descriptions-item>
        <el-descriptions-item label="邮件参数">{{ detailData.templateParams }}</el-descriptions-item>
        <el-descriptions-item label="发送返回的消息" :span="2">{{ detailData.sendMessageId }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script setup name="MailLog" lang="ts">
import { listMailLog, getMailLog } from '@/api/mail/log';
import { MailLogVO, MailLogQuery, MailLogForm } from '@/api/mail/log/types';
import { ComponentInternalInstance } from 'vue';
import { ElForm } from 'element-plus';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { sys_mail_status } = toRefs<any>(proxy?.useDict('sys_mail_status'));

const mailLogList = ref<MailLogVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const daterangeSendTime = ref([]);
const dialogVisible = ref(false) // 弹窗的是否展示
const detailLoading = ref(false) // 表单的加载中

const queryFormRef = ref(ElForm);
const mailLogFormRef = ref(ElForm);


const initFormData: MailLogForm = {
  id: '',
  toMail: '',
  accountId: '',
  fromMail: '',
  templateId: '',
  templateCode: '',
  templateNickname: '',
  templateTitle: '',
  templateContent: '',
  templateParams: '',
  sendStatus: '',
  sendTime: '',
  sendMessageId: '',
  sendException: '',
}
const data = reactive<PageData<MailLogForm, MailLogQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    accountId: '',
    templateId: '',
    sendStatus: '',
    sendTime: '',
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

const detailData = ref({
  id: '',
  sendTime: '',
  toMail: '',
  sendStatus: '',
  sendException: '',
  fromMail: '',
  templateId: '',
  templateNickname: '',
  templateContent: '',
  templateParams: '',
  sendMessageId: '',
}) // 详情数据

/** 查询邮件日志列表 */
const getList = async () => {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeSendTime && '' != daterangeSendTime) {
    queryParams.value.params["beginSendTime"] = daterangeSendTime.value[0];
    queryParams.value.params["endSendTime"] = daterangeSendTime.value[1];
  }
  const res = await listMailLog(queryParams.value);
  mailLogList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  daterangeSendTime.value = [];
  queryFormRef.value.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: MailLogVO[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 打开弹窗 */
const open = async (id: number) => {
  dialogVisible.value = true
  // 设置数据
  detailLoading.value = true
  try {
    const res = await getMailLog(id)
    detailData.value = res.data
  } finally {
    detailLoading.value = false
  }
}

onMounted(() => {
  getList();
});
</script>
